<?php

namespace App\Admin\Controllers;

use App\Admin\Tools\DeleteUser;
use App\Admin\Tools\Clear;
use App\Models\Member;

use App\Models\MyCar;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;

class MemberController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {


            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {


            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {


            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Member::class, function (Grid $grid) {
            $grid->model()->orderByDesc('uid');
            $grid->uid('UID')->sortable();
            $grid->avatar('头像')->image('',50,50);
            $grid->nickname('用户名');
            $grid->P_credit('金币')->sortable();
            $grid->level('等级')->sortable();
            $grid->cars('车辆数')->display(function(){
                return MyCar::where('uid',$this->uid)->count();
            })->badge('info');
    
            $grid->disableCreateButton();
            $grid->disableExport();
            //$grid->disableRowSelector();
            $grid->actions(function($actions) {
                $actions->disableDelete();
            });
            $grid->filter(function($filter){
        
                $filter->like('nickname', '昵称');
                $filter->gt('P_credit', '金币余额');
            });
            
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                    $batch->add('删除用户', new DeleteUser());
                });
            });
            
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Member::class, function (Form $form) {

            $form->display('uid', 'ID');
            $form->display('remote_user_id', 'APP用户ID');
            $form->number('P_credit','金币数量')->rules('max:999999999');
            $form->number('level','用户等级')->rules('max:10');
            
        });
    }
    
    public function deleteUser(Request $request)
    {
        return collect($request->input('ids'))->map(function($id){
            Clear::user($id);
        });
        
        
        
    }
}
